/**
 * Socket客户端管理模块
 */
class SocketClient {
    constructor() {
        this.socket = null;
        this.isConnected = false;
        this.layer = layui.layer;
        this.eventHandlers = {};
        this.init();
    }

    init() {
        this.socket = io();
        this.setupEventListeners();
    }

    setupEventListeners() {
        this.socket.on('connect', () => {
            this.isConnected = true;
            console.log('已连接到服务器');
            window.showStatusMessage('已连接到服务器', 'success');
        });

        this.socket.on('disconnect', () => {
            this.isConnected = false;
            console.log('与服务器断开连接');
            window.showStatusMessage('与服务器断开连接', 'error');
        });

        this.socket.on('connection_result', (data) => {
            console.log('连接结果:', data);
            if (data.success) {
                this.layer.msg('远程主机登录成功', {icon: 1});
                window.showStatusMessage('登录成功', 'success');
            } else {
                this.layer.msg('远程主机登录失败', {icon: 2});

                window.showStatusMessage('登录失败：' + data.msg, 'error');
            }
        });

        this.socket.on('event_result', (data) => {
            console.log('事件结果:', data);

            if (data.success) {
                if (data.cmd_event == 'tree') {
                    window.TreeManager.handleEventResult(data);
                    
                } else if (data.cmd_event == 'detail') {
                    window.DetailManager.handleEventResult(data);
                    
                } else if (data.cmd_event == 'read' || data.cmd_event == 'write') {
                    window.EditorManager.handleEventResult(data);
                } else if (data.cmd_event == 'rename') {
                    window.DetailManager.handleEventResult(data);
                } else if (data.cmd_event == 'mkdir') {
                    window.DetailManager.handleEventResult(data);
                } else if (data.cmd_event == 'touch') {
                    window.DetailManager.handleEventResult(data);
                } else if (data.cmd_event == 'delete') {
                    window.DetailManager.handleEventResult(data);
                } else if (data.cmd_event == 'move') {
                    window.DetailManager.handleEventResult(data);
                } else if (data.cmd_event == 'copy') {
                    window.DetailManager.handleEventResult(data);
                } else if (data.cmd_event == 'chmod') {
                    window.DetailManager.handleEventResult(data);
                } else {
                    this.layer.msg('事件执行成功', {icon: 1});
                    window.showStatusMessage(data.msg, 'success');
                }
            } else {
                this.layer.msg('事件执行失败', {icon: 2});
                window.showStatusMessage(data.msg, 'error');
            }
            
        });
    }

    // 发送登录请求
    loginRemote(config) {
        if (!this.isConnected) {
            console.error('Socket未连接');
            return;
        }
        this.socket.emit('login_remote', config);
    }

    // 发送命令事件
    sendCommand(config) {
        if (!this.isConnected) {
            console.error('Socket未连接');
            return;
        }
        this.socket.emit('command_event', config);
    }

    // 事件监听（外部调用）
    on(event, handler) {
        this.socket.on(event, handler);
    }

    // 触发事件（外部调用）
    emit(event, data) {
        this.socket.emit(event, data);
    }

}

// 创建全局Socket客户端实例
window.SocketClient = new SocketClient();